package com.nf.health.app.activity.doctor_advice.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.doctor_advice.activity.MyDoctorAdaviceDetailActivity;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.CircleImageView;
import com.nf.health.app.customview.SwipeView;
import com.nf.health.app.customview.SwipeView.OnSwipeStatusChangeListener;
import com.nf.health.app.models.DoctorAdviceItem;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.ImageLoaderUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 医嘱列表
 */
public class DoctorAdviceAdapter  extends BaseAdapter{

	int i;

	private ArrayList<SwipeView> unClosedSwipeView = new ArrayList<SwipeView>();

	private Context context;

	Activity activity;

	private List<DoctorAdviceItem> datas;
	public DoctorAdviceAdapter(Context context,
							   List<DoctorAdviceItem> datas) {
		this.context = context;
		this.datas = datas;
		activity = (Activity) context;
	}

	@Override
	public int getCount() {
		if(datas!=null&&datas.size()>0){
			return datas.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int arg0) {
		return datas.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position, View convertview, ViewGroup arg2) {
		ViewHolder holder = null;
		DoctorAdviceItem doctorAdviceItem = datas.get(position);
		if (convertview==null) {
			convertview = View.inflate(context, R.layout.item_doctoradvice_new, null);
			holder = new ViewHolder();
			holder.tv_title = (TextView) convertview.findViewById(R.id.tv_adview_title);//医嘱title
			holder.doctor_advice_name = (TextView) convertview.findViewById(R.id.doctor_advice_name);//医生姓名
			holder.docadview_right_image = (ImageView) convertview.findViewById(R.id.docadview_right_image);//未读消息提示
			holder.iv_doctor_image = (CircleImageView) convertview.findViewById(R.id.iv_doctor_image);//医生头像
			holder.tv_time = (TextView) convertview.findViewById(R.id.tv_time);//时间
			holder.swipeView  = (SwipeView) convertview.findViewById(R.id.swipeView);
			holder.rl_content  = (RelativeLayout) convertview.findViewById(R.id.rl_content);
			holder.delete  = convertview.findViewById(R.id.delete);
			convertview.setTag(holder);
		} else {
			holder = (ViewHolder) convertview.getTag();
		}

//		if(doctorAdviceItem.getStatus().equals("n")){
//			holder.docadview_right_image.setVisibility(View.VISIBLE);
//		}else{
//			holder.docadview_right_image.setVisibility(View.GONE);
//		}
//		判断是否隐藏
		holder.docadview_right_image.setVisibility(View.GONE);//最新版去掉此功能


		holder.doctor_advice_name.setText(doctorAdviceItem.getDoctorname());
		ImageLoaderUtils.getinstance(context).getImage(holder.iv_doctor_image, doctorAdviceItem.getDoctorhimg(), R.drawable.default_head);
		String time = DateUtil.date2String(TextUtils.isEmpty(doctorAdviceItem.getCreatetime()+"") ? System.currentTimeMillis() : Long.parseLong(String.valueOf(doctorAdviceItem.getCreatetime())), "yyyy-MM-dd");
		holder.tv_time.setText(time);
		holder.tv_title.setText("医嘱报告");






		holder.rl_content.setTag(position);
		holder.rl_content.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				/*closeAllOpenedSwipeView();*/
				int position = (Integer) v.getTag();
				Bundle bundle = new Bundle();
				String autoid = datas.get(position).getId();
				bundle.putString(Constants.DOCTOR_ADVICE_ID,autoid);
    				/*bundle.putString(WebViewActivity.KEY_URL, Apis.HOST_COMMON+"/"+autoid+"/index.html");
    				bundle.putString(WebViewActivity.KEY_TITLE,"检测报告");*/
				ActivityUtils.showActivity(activity,MyDoctorAdaviceDetailActivity.class, bundle);//医嘱详情页
			}
		});

		holder.delete.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(listener != null)
					listener.onClick(position, "");
				unClosedSwipeView.clear();
			}
		});
		/**
		 * 2.2的版本不用做删除
		 */
//		holder.swipeView.setOnSwipeStatusChangeListener(new OnSwipeStatusChangeListener() {
//
//			@Override
//			public void onSwiping(SwipeView swipingSwipeView) {
//				if (!unClosedSwipeView.contains(swipingSwipeView)) {
//					closeAllOpenedSwipeView();
//				}
//				unClosedSwipeView.add(swipingSwipeView);
//			}
//
//			@Override
//			public void onOpen(SwipeView openedSwipeView) {
//				for (int i = 0; i < unClosedSwipeView.size(); i++) {
//					if (unClosedSwipeView.get(i) != openedSwipeView) {
//						unClosedSwipeView.get(i).close();
//					}
//				}
//				if (!unClosedSwipeView.contains(openedSwipeView)) {
//					unClosedSwipeView.add(openedSwipeView);
//				}
//			}
//
//			@Override
//			public void onClose(SwipeView closedSwipeView) {
//				unClosedSwipeView.remove(closedSwipeView);
//			}
//		});


		return convertview;
	}
	static class ViewHolder{
		TextView tv_title,tv_time,doctor_advice_name;
		SwipeView swipeView;
		View delete;
		ImageView docadview_right_image;
		CircleImageView iv_doctor_image;
		private RelativeLayout rl_content;

	}


	private OnMyClickListener listener;
	public void setOnMyClickListener(OnMyClickListener listener){
		this.listener = listener;
	}
	private void closeAllOpenedSwipeView() {
		for (int i = 0; i < unClosedSwipeView.size(); i++) {
			if (unClosedSwipeView.get(i).getSwipeStatus() != SwipeView.SwipeStatus.Close) {
				unClosedSwipeView.get(i).close();
			}
		}
	}
}
